const multer = require('@koa/multer');
const moment = require('moment');
const path = require('path');
let firstName = 'file'
//因为bodyparse不支持multipart/form-data，所以采用multer
//上传文件存放路径、及文件命名
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname ,'../public/uploads'))
    },
    filename: function (req, file, cb) {
        let type = file.originalname.split('.')[1]
        let name = file.originalname.split('.')[0]
        // currentDateStr = moment(new Date()).format('YYYY-MM-DD')
        currentDateStr = new Date().getTime()
        // console.log(11111111111111111111111,`${firstName}-${name}-${currentDateStr}.${type}`)
        cb(null, `${firstName}-${name}-${currentDateStr}.${type}`)
    }
})
//文件上传限制
const limits = {
    fields: 20,//非文件字段的数量
    fileSize: 500 * 1024,//文件大小 单位 b
    files: 10//文件数量
}
// 对上传的文件进行过滤
const fileFilter = (req, file, cb) => {
	// const type = path.extname(file.originalname)
    let type = file.originalname.split('.')[1]
	if (type == 'png'||type=='jpg') {
		cb(null, true)
	} else {
		// req.failFiles.push(file.originalname)
		cb(null, false)
	}
}
const uploadLocal = multer({storage,limits});
module.exports = uploadLocal